package com.ayu.shortlink.settlement.service.handler.strategy;

import com.ayu.shortlink.settlement.common.convention.exception.ClientException;
import com.ayu.shortlink.settlement.common.enums.DiscountTypeEnum;
import com.ayu.shortlink.settlement.service.bases.strategy.AbstractDiscountStrategy;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 达到金额满减折扣策略
 */
@Component
public class ThresholdDiscountStrategy implements AbstractDiscountStrategy {
    @Override
    public BigDecimal handler(BigDecimal totalAmount, Map consumeRule) {
        BigDecimal atLeastMoney = (BigDecimal)consumeRule.get("atLeastMoney");
        if (atLeastMoney == null || totalAmount.compareTo(atLeastMoney) < 0){
            throw new ClientException("该优惠券未达到最低金额使用门槛");
        }
        BigDecimal couponAmount = (BigDecimal)consumeRule.get("couponAmount");
        // 进行金额校验
        if (couponAmount == null || couponAmount.compareTo(BigDecimal.ZERO) < 0){
            throw new ClientException("该优惠券扣减金额有问题");
        }
        return totalAmount.subtract(couponAmount);
    }

    @Override
    public Integer mark() {
        return DiscountTypeEnum.THRESHOLD_DISCOUNT.getType();
    }
}
